<template>
  <div class="resource-content">
    <div class="resource-content-left">
      <!-- draggable -->
      <el-tree
        :data="categoryTreeData"
        default-expand-all
        :icon="getSvgIcon('ic_left')"
        :props="{
          children: 'children',
          label: 'categoryName',
        }"
        @node-click="nodeClick"
        :allow-drag="allowDrag"
        @node-drag-start="handleDragStart"
        @node-drag-enter="handleDragEnter"
        @node-drag-leave="handleDragLeave"
        @node-drag-over="handleDragOver"
        @node-drag-end="handleDragEnd"
        @node-drop="handleDrop"
      >
        <template #default="{ node, data }">
          <span class="custom-tree-node">
            <span
              class="custom-icon active"
              :class="['模型', '底板'].includes(node.label) ? 'no-bg' : ''"
            >
              <SvgIcon
                :icon-class="
                  node.label === '模型' ? 'ic_模型' : node.label === '底板' ? 'ic_底板' : ''
                "
              />
            </span>
            <span v-if="data.isLocked !== 1" @ondblclick="editChange(data)">{{ node.label }}</span>
            <span v-else>{{ node.label }}</span>
            <div class="operate">
              <!-- 模型/未分类”这个二级目录不可新建三级目录。底板模块不支持三级目录 -->
              <span class="add" v-if="data.level === 3"></span>
              <span
                class="add"
                v-else-if="data.parentId === '51e428d9-c2bd-45ab-bd6c-21eebd8d5aba'"
              >
              </span>
              <span
                class="add"
                v-else-if="
                  [
                    '62b5ef11-c436-4bb6-bf11-66c08bec73fe',
                    '51e428d9-c2bd-45ab-bd6c-21eebd8d5aba',
                  ].includes(data.categoryId)
                "
              >
              </span>

              <span class="add" @click="addCategory(data)" v-else>
                <SvgIcon icon-class="ic_加"></SvgIcon>
              </span>

              <span class="del" v-if="data.isLocked !== 1" @click="delCategoryChange(data)">
                <!-- <el-icon><Delete /></el-icon> -->
                <SvgIcon icon-class="delete"></SvgIcon>
              </span>
            </div>
          </span>
        </template>
      </el-tree>
    </div>
    <div class="resource-content-right">
      <div class="content-right-search">
        <el-input
          v-model="formData.blurName"
          :placeholder="`请在 “${currentTreeNode.categoryName}” 内搜索`"
          :prefix-icon="getSvgIcon('ic_搜索')"
          @change="search({ blurName: formData.blurName, categoryId: currentTreeNode.categoryId })"
        >
        </el-input>
        <el-button color="#0560FD" @click="updateChange">上传</el-button>
      </div>
      <div class="content-right-list">
        <div class="breadcrumbs-show">
          <div class="breadcrumbs">
            <span class="title">{{ currentTreeNode.categoryName }}</span>
            <span class="num" v-if="checkBoxArr.length">
              （{{ checkBoxArr.length }}/{{ count }}）
            </span>
            <span class="num" v-else>（{{ count }}）</span>
          </div>
          <div class="show">
            <SvgIcon
              @click="toggleChange"
              :icon-class="isThumbnail ? 'ic_列表' : 'ic_图表'"
            ></SvgIcon>
          </div>
        </div>
        <ThumbnailList
          v-loading="loading"
          :is-thumbnail="isThumbnail"
          :data="tableData"
          @check-change="checkChange"
          @operational-data-change="createCloseHandle"
          @edit-dialog="editDialog"
        ></ThumbnailList>
      </div>
    </div>
  </div>
  <!-- 新建分类弹框 -->
  <el-dialog v-model="addCategoryVisible" title="新建子分类" align-center class="category-model">
    <div class="category-input">
      <label>名称</label>
      <el-input v-model="categoryName" placeholder="请输入新建子分类名称"></el-input>
    </div>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="addCategoryVisible = false" class="cancel">取消</el-button>
        <el-button
          :class="categoryName !== '' ? 'active' : ''"
          type="primary"
          @click="addCategoryChange()"
          class="category"
          :disabled="categoryName === ''"
        >
          创建
        </el-button>
      </span>
    </template>
  </el-dialog>

  <!-- 修改分类弹框 -->
  <el-dialog v-model="renameVisible" title="重命名" align-center class="category-model">
    <div class="category-input">
      <label>名称</label>
      <el-input v-model="renameName" placeholder="请输入分类名称"></el-input>
    </div>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="renameVisible = false" class="cancel">取消</el-button>
        <el-button type="primary" @click="reNameChange" class="determine"> 确定 </el-button>
      </span>
    </template>
  </el-dialog>

  <!-- 移动成功弹出层 -->
  <div class="move-popup" v-if="isMobileSuccessful">
    <!-- <el-icon><CircleCheckFilled /></el-icon> -->
    <SvgIcon icon-class="M-dui"></SvgIcon>
    <span class="text">移动成功</span>
    <!-- <el-button color="#0560FD" plain @click="revokeChange">撤销</el-button> -->
  </div>

  <!-- 正在移动 -->
  <div class="moving" v-if="isMoving">
    <span class="loading-custom" v-loading="true"></span>
    <span class="text">正在移动</span>
  </div>

  <!-- 弹框 -->
  <CreateDialog
    v-if="createDialogShow"
    :resource="selRow"
    :is-re-upload="isReUpload"
    :is-include-current="true"
    @close-handle="createCloseHandle"
  ></CreateDialog>
</template>

<script setup lang="ts">
import { SvgIcon } from '@jl/common';
import { useTree } from './composables/tree';
import { useList } from './composables/useList';
import { IResource } from '@/api/resource/types';
import { ElMessage, ElMessageBox } from 'element-plus';
import ThumbnailList from './components/thumbnail-list.vue';
import CreateDialog from '@/views/resource/create/index.vue';
import { useRequest } from '@jl/common/composable';
// import { useResourceStore } from '@/store/resource';
import { getSvgIcon } from '@/components/svg-icon';

import { createResourceCategory, delResourceCategory, editResourceCategory } from '@/api/resource';
defineOptions({
  name: 'ResourceIndex',
});
// const resourceStore = useResourceStore();
// 导入tree方法
const {
  categoryTreeData,
  mobileSuccessfulFlag,
  dragStart,
  allowDrag,
  handleDragStart,
  handleDragEnter,
  handleDragLeave,
  handleDragOver,
  handleDragEnd,
  handleDrop,
  getList,
} = useTree();
const currentTreeNode = ref<any>({});
const numTime = ref<number>(0);
const nodeClick = (node: any) => {
  numTime.value++;
  setTimeout(() => {
    if (numTime.value === 1) {
      currentTreeNode.value = node;
      formData.value.blurName = '';
      search({ categoryId: currentTreeNode.value.categoryId });
    }
    if (numTime.value === 2) {
      editChange(node);
    }
    numTime.value = 0;
  }, 300);
};
// 移动
const isMobileSuccessful = ref(false);
// const revokeChange = () => {
//   console.log('撤销');
// };

watch(
  () => mobileSuccessfulFlag.value,
  () => {
    if (mobileSuccessfulFlag.value) {
      isMoving.value = false;
      isMobileSuccessful.value = true;
      setTimeout(() => {
        isMobileSuccessful.value = false;
      }, 3000);
    }
  }
);
// 移动中
const isMoving = ref(false);
watch(
  () => dragStart.value,
  () => {
    if (dragStart.value) {
      mobileSuccessfulFlag.value = false;
      isMoving.value = true;
    }
  }
);
const checkChange = (data: any) => {
  checkBoxArr.value = data;
};
// 新建子分类
const { run, data } = useRequest<any, any>(createResourceCategory, {});
const addCategoryVisible = ref(false);
const categoryName = ref('');
const currentCategoryObj = ref<any>({});
const addCategory = (item: any) => {
  addCategoryVisible.value = true;
  currentCategoryObj.value = item;
};
const addCategoryChange = async () => {
  const { categoryId } = currentCategoryObj.value;
  const params = { parentId: categoryId, categoryName: categoryName.value };
  const res = await run({ params });
  console.log(res);
  addCategoryVisible.value = false;
  categoryName.value = '';
  if (delData.value) {
    getList();
    ElMessage({
      message: '添加成功！',
      type: 'success',
    });
  }
};

// 删除子分类
const { run: delRun, data: delData } = useRequest<any, any>(delResourceCategory, {});
const delCategoryChange = (data: any) => {
  ElMessageBox.confirm('确定要删除吗?', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  }).then(async () => {
    const params = { categoryId: data.categoryId, delFile: true };
    await delRun({ params });
    if (delData.value) {
      getList();
      ElMessage({
        type: 'success',
        message: '删除成功！',
      });
    }
  });
};

// 修改重命名子分类
const { run: editRun, data: editData } = useRequest<any, any>(editResourceCategory, {});
const editChange = (data: any) => {
  currentCategoryObj.value = data;
  renameName.value = data.categoryName;
  renameVisible.value = true;
};

const renameVisible = ref(false);
const renameName = ref('');
const reNameChange = async () => {
  renameVisible.value = false;
  const params = {
    categoryId: currentCategoryObj.value.categoryId,
    categoryName: renameName.value,
  };
  await editRun({ params });
  if (editData.value) {
    getList();
    ElMessage({
      message: '修改成功！',
      type: 'success',
    });
  }
};

// 导入列表方法
const { formData, isThumbnail, tableData, count, loading, toggleChange, search, checkBoxArr } =
  useList();
const createDialogShow = ref(false);
const isReUpload = ref(false);
const selRow = ref<null | IResource>(null);
const updateChange = () => {
  isReUpload.value = false;
  selRow.value = null;
  createDialogShow.value = true;
};

watch(
  () => categoryTreeData.value,
  (res) => {
    if (res.length) {
      currentTreeNode.value = res[0];
    }
  },
  {
    deep: true,
    immediate: true,
  }
);

onMounted(() => {
  search();
});

const createCloseHandle = (refresh?: boolean) => {
  createDialogShow.value = false;
  if (refresh) {
    search({ categoryId: currentTreeNode.value.categoryId });
  }
};
const editDialog = (data: any) => {
  selRow.value = data.selRow;
  isReUpload.value = data.isReUpload;
  createDialogShow.value = true;
};
</script>

<style lang="less" scoped>
.resource-content {
  display: flex;
  flex-direction: row !important;
  .resource-content-left {
    overflow: auto;
    width: 280px;
    padding-right: 24px;
    border-right: 2px solid fade(#282828, 8%);
    :deep(.el-tree) {
      .el-tree-node__content {
        height: 28px;
        line-height: 28px;
        margin-bottom: 10px;
        :deep(.is-expanded) {
          .el-tree-node__content {
            .custom-tree-node {
              .active {
                background: url('~@/assets/png/tree-expand.png') no-repeat center;
                background-size: 18px 18px;
                &.no-bg {
                  background: none !important;
                }
              }
            }
          }
        }
        .custom-tree-node {
          display: flex;
          align-items: center;
          width: 100%;
          .active {
            background: url('~@/assets/png/tree-close.png') no-repeat center;
            background-size: 18px 18px;
            &.active.no-bg {
              background: none !important;
            }
          }
          .custom-icon {
            font-size: 20px;
            margin-right: 6px;
            margin-left: 4px;
            color: fade(#161616, 65%);
          }
          .operate {
            display: flex;
            justify-content: center;
            align-items: center;

            margin-left: auto;
          }
          &:hover .add,
          &:hover .del {
            display: block;
          }
          .add,
          .del {
            display: none;
            font-size: 16px;
            font-weight: bold;
            color: #0560fd;
            fill: #0560fd;
            line-height: 1em;
            margin-left: auto;
            margin-right: 8px;
          }
        }
      }
      .el-tree-node__expand-icon {
        font-weight: bold;
        font-size: 14px;
        padding: 6px 0;
      }
      .is-current {
        & > .el-tree-node__content {
          font-size: 14px;
          color: #161616;
          font-weight: bold;
          border-radius: 4px;
          border: 1px solid fade(#0560fd, 100%);
          background-color: fade(#0560fd, 36%);
          .custom-icon {
            color: fade(#161616, 100%);
          }
        }
      }
      .el-tree-node__expand-icon.is-leaf {
        background: none !important;
        color: transparent;
      }
    }
  }
  .resource-content-right {
    padding-left: 24px;
    flex: 1;
    display: flex;
    flex-direction: column;
    .content-right-search {
      width: 100%;
      height: 48px;
      padding: 8px 16px;
      background: fade(#282828, 8%);
      border-radius: 4px;
      display: flex;
      justify-content: space-between;
      .el-input {
        width: 360px;
      }
      :deep(.el-input-group__prepend) {
        background-color: #fff;
        padding: 0 10px;
        border-top-left-radius: 4px;
        border-bottom-left-radius: 4px;
        svg.svg-icon {
          fill: #9a9a9a;
        }
      }
      .el-button {
        height: 32px;
        width: 80px;
      }
    }
    .content-right-list {
      flex: 1;
      display: flex;
      flex-direction: column;
      overflow: hidden;
    }
    .breadcrumbs-show {
      display: flex;
      justify-content: space-between;
      padding: 10px 0;
      font-size: 14px;
      .breadcrumbs {
        .title {
          color: #9a9a9a;
        }
        .num {
          color: #161616;
        }
      }
      .show {
        font-size: 24px;
        cursor: pointer;
        .svg-icon {
          fill: #9a9a9a;
        }
      }
    }
  }
}
.move-popup {
  padding: 0 17px;
  display: flex;
  flex-direction: row !important;
  align-items: center;
  position: absolute;
  left: 50%;
  top: 70px;
  transform: translateX(-50%);
  width: 194px;
  height: 40px !important;
  background-color: fade(#24bd48, 16%);
  border: 1px solid fade(#24bd48, 48%);
  border-radius: 2px;
  .el-icon {
    color: #24bd48;
    margin-top: 3px;
  }
  span.text {
    margin-left: 8px;
    color: #161616;
  }
  .el-button {
    height: 24px;
    width: 49px;
    margin-top: 3px;
    margin-left: auto;
    color: #0560fd;
    background-color: fade(#0560fd, 30%);
    border: 1px solid fade(#0560fd, 100%);
  }
}
.moving {
  display: flex;
  flex-direction: row !important;
  align-items: center;
  position: absolute;
  left: 50%;
  top: 70px;
  transform: translateX(-50%);
  width: 194px;
  height: 40px !important;
  background-color: fade(#0560fd, 16%);
  border: 1px solid fade(#0560fd, 68%);
  padding: 0 14px;
  border-radius: 2px;
  .loading-custom {
    display: inline-block;
    width: 20px;
    height: 20px;
    background-color: transparent;
    margin-right: 10px;
    :deep(.el-loading-mask) {
      background-color: transparent !important;
    }
  }
  :deep(.el-loading-spinner) .circular {
    width: 20px;
  }
  .text {
    font-size: 14px;
    color: #161616;
  }
}
.operate {
  height: 60px;
  .el-button {
    width: 80px;
    height: 32px;
  }
  .el-button--primary.is-plain,
  .el-button--danger.is-plain {
    background-color: transparent;
    border: 1px solid #0560fd;
    &:hover {
      color: #0560fd;
    }
  }
  .el-button--danger.is-plain {
    background-color: transparent;
    border: 1px solid #ff5454;
    &:hover {
      color: #ff5454;
    }
  }
}
</style>
<style lang="less">
.category-model {
  width: 420px;
  .el-dialog__header {
    font-size: 24px;
    padding-bottom: 5px;
    font-weight: 500;
    color: #585858;
    padding-top: 16px;
  }
  .el-dialog__body {
    height: auto;
    padding: 16px;
    padding-top: 0;
    .category-input {
      label {
        display: block;
        font-weight: 500;
        font-size: 14px;
        height: 38px;
        color: #585858;
        line-height: 38px;
      }
      .el-input {
        height: 32px;
        border-radius: 4px;
      }
    }
  }
  .el-dialog__footer {
    text-align: right;
    padding-bottom: 16px;
    .el-button {
      height: 26px;
      padding: 0;
      padding-left: 10px;
      padding-right: 0px;
      width: auto;
      letter-spacing: 10px;
      border-radius: 2px;
      width: 65px;
      &.cancel {
        color: #000000;
        border: 1px solid #d9d9d9;
      }
      &.category {
        color: #ffffff;
        border: none;
        background: fade(#9a9a9a, 50%);
        box-shadow: none;
        &.active {
          background-color: #0560fd;
          color: #fff;
        }
      }
    }
  }
}
</style>
